package com.grad.webstudy.common;

import com.grad.webstudy.constants.TokenConstants;
import com.grad.webstudy.constants.UserContants;
import com.grad.webstudy.context.UserContext;
import com.grad.webstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @Description: 在项目启动时清除登录的redis缓存
 * @Author: zjf
 * @Date: 2020/1/21 10:30.
 */
@Component
public class ClearCacheLoginOnStart implements CommandLineRunner {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public void run(String... args) throws Exception {
        String pattern = TokenConstants.TOKEN_LOGIN_CACHE_KEY_PREFIX + "*";
        //暂时只清除token相关的缓存 todo:以后可能会更改为多前缀匹配
        Set<String> keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
